package com.yazi.mxz.common.db.dao.impl;


import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.hibernate.Criteria;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Repository;

import com.yazi.mxz.common.db.dao.RoleDao;
import com.yazi.mxz.common.entity.beans.Company;
import com.yazi.mxz.common.entity.beans.Role;
import com.yazi.mxz.common.entity.beans.Store;

import com.myframework.smhj.hibernate.dao.BasicDao;

@Repository("roleDao")
public class RoleDaoImpl extends BasicDao<Role> implements RoleDao {

	
	public RoleDaoImpl() {
		super(Role.class);
		
	}

	@Override
	public Criteria getExample(Role role, String searchWord) {
		Criteria c = super.getExample(role, searchWord);
		if (null != role) {
			// 店铺
			Store store = role.getStore();
			if (null != store) {
				// 店铺ID
				if (null != store.getStoreId()) {
					c.add(Restrictions.eq("store.storeId", store.getStoreId()));
				} else {
					// ...店铺其它条件
				}
			}
			// 企业
			Company company = role.getCompany();
			if (null != company) {
				// 企业ID
				if (null != company.getCompanyId()) {
					c.add(Restrictions.eq("company.companyId", company.getCompanyId()));
				} else {
					// ...企业其它条件
				}
			}
			
		}
		//关键字查询
		if (null != searchWord && !searchWord.isEmpty()) {
			String likeWord = "%" + searchWord + "%";
			c.add(Restrictions.or(Restrictions.like("name", likeWord)));
		}
		return c;
	}

	@Override
	public List<Role> listByAdminId(long adminId) {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("adminId", adminId);
		return list("listByAdminIdHql", map);
	}

	@Override
	public List<Role> listByMemberId(long memberId) {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("memberId", memberId);
		return list("listByMemberIdHql", map);
	}
	
}
